package com.dongdong.bookstore.module.ums.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dongdong.bookstore.common.api.CommonPage;
import com.dongdong.bookstore.common.exception.Asserts;
import com.dongdong.bookstore.module.ums.model.Menu;
import com.dongdong.bookstore.module.ums.mapper.MenuMapper;
import com.dongdong.bookstore.module.ums.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dongdong.bookstore.module.ums.service.UmsCacheService;
import com.dongdong.bookstore.module.ums.viewmodel.MenuNode;
import com.dongdong.bookstore.module.ums.viewmodel.MenuParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单表  服务实现类
 * </p>
 *
 * @author chiangkai
 * @since 2021-10-20
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private UmsCacheService umsCacheService;

    @Override
    public List<Menu> getMenuListByAdminId(Long id) {
        List<Menu> menuList = umsCacheService.getMenuListByAdminId(id);
        if (CollUtil.isEmpty(menuList)) {
            menuList = menuMapper.getMenuListByAdminId(id);
            if (CollUtil.isNotEmpty(menuList)) {
                umsCacheService.setMenuListByAdminId(id, menuList);
            }
        }
        return menuList;
    }

    @Override
    public void create(MenuParam menu) {
        Menu target = new Menu();
        BeanUtils.copyProperties(menu, target);
        updateMenuLevel(target);
        save(target);
    }

    @Override
    public void update(Long id, MenuParam menuParam) {
        Menu menu = new Menu();
        BeanUtils.copyProperties(menuParam, menu);
        menu.setId(id);
        updateMenuLevel(menu);
        Asserts.isFalse(updateById(menu), "更新菜单失败");
    }

    @Override
    public Page<Menu> list(Long parentId, Integer pageSize, Integer pageNum) {
        Page<Menu> menuPage = new Page<>(pageNum, pageSize);
        QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
        menuQueryWrapper.lambda().eq(Menu::getParentId, parentId).orderByDesc(Menu::getSort);
        return page(menuPage, menuQueryWrapper);
    }

    @Override
    public List<MenuNode> treeList() {
        List<Menu> list = list();
        return list.stream().filter(menu -> menu.getParentId().equals(0L))
                .map(topMenu->covertMenuNode(topMenu,list)).collect(Collectors.toList());
    }

    @Override
    public void updateHidden(Long id, Integer hidden) {
        Menu menu = new Menu();
        menu.setId(id);
        menu.setHidden(hidden);
        Asserts.isFalse(updateById(menu),"更新显示状态失败");
    }

    private MenuNode covertMenuNode(Menu topMenu, List<Menu> list) {
        MenuNode menuNode = new MenuNode();
        BeanUtils.copyProperties(topMenu, menuNode);
        List<MenuNode> collect = list.stream().filter(submenu -> submenu.getParentId().equals(topMenu.getId())).map(submenu -> covertMenuNode(submenu, list)).collect(Collectors.toList());
        menuNode.setChildren(collect);
        return menuNode;
    }


    /**
     * 修改子菜单的Level 根据父ID
     *
     * @param subMenu 菜单
     */
    private void updateMenuLevel(Menu subMenu) {
        if (subMenu.getParentId() == 0) {
            //没有父菜单时为一级菜单
            subMenu.setLevel(0);
        } else {
            //有父菜单时选择根据父菜单level设置
            Menu parentMenu = getById(subMenu.getParentId());
            if (parentMenu != null) {
                subMenu.setLevel(parentMenu.getLevel() + 1);
            } else {
                subMenu.setLevel(0);
            }
        }
    }


}
